﻿#include "RecordOfCharge.h"
#include "ui_recordofcharge.h"

RecordOfCharge::RecordOfCharge(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::RecordOfCharge)
{
    ui->setupUi(this);
    time = new QLabel(ui->frameBottom);
    time->setGeometry(10, 10, 200, 40);
    time->setStyleSheet("QLabel{font: 20px; color: rgb(25, 25, 112);}");
    signal = new QLabel(ui->frameBottom);
    signal->setGeometry(700, 18, 41, 22);
    signal->setStyleSheet("QLabel{background-color:transparent;}");

    QDir dir(IMAGE_PATH);
    if(dir.exists())
    {
        ui->frameTop->setStyleSheet(LOGO_STY);
    }

    Fitrating = 0;
    ROC_accountModel = new QSqlQueryModel(this);
    ROC_accountMapper = new QDataWidgetMapper(this);
    ROC_accountMapper->setModel(ROC_accountModel);
    ROC_accountMapper->setSubmitPolicy(QDataWidgetMapper::AutoSubmit);
//    connect(ROC_accountMapper, SIGNAL(currentIndexChanged(int)), this, SLOT(onAccountCuttentIndex(int)));


    tabWidgetChargingRecord.setParent(ui->frameMid);
    tabWidgetChargingRecord.move(20, 50);
    QFont font = tabWidgetChargingRecord.horizontalHeader()->font();
    font.setBold(true);
    tabWidgetChargingRecord.horizontalHeader()->setFont(font);
    tabWidgetChargingRecord.setColumnCount(8);
    tabWidgetChargingRecord.setRowCount(6);
    tabWidgetChargingRecord.setShowGrid(true);
    tabWidgetChargingRecord.setSelectionMode(QAbstractItemView::NoSelection);
    tabWidgetChargingRecord.setEditTriggers(QAbstractItemView::NoEditTriggers);
    tabWidgetChargingRecord.setDragDropOverwriteMode(false);
    tabWidgetChargingRecord.horizontalHeader()->setStretchLastSection(true);
    tabWidgetChargingRecord.setFrameShape(QFrame::NoFrame);
    tabWidgetChargingRecord.setFixedSize(760, 240);
    tabWidgetChargingRecord.setColumnWidth(0, 30);
    tabWidgetChargingRecord.setColumnWidth(1, 220);
    tabWidgetChargingRecord.setColumnWidth(2, 100);
    for(int i=3; i<6; i++)
    {
        tabWidgetChargingRecord.setColumnWidth(i, 70);
    }

    for(int i=6; i<8; i++)
    {
        tabWidgetChargingRecord.setColumnWidth(i, 80);
    }

    tabWidgetChargingRecord.setFocusPolicy(Qt::NoFocus);

    for(int i=0 ;i<6; i++)
    {
        tabWidgetChargingRecord.setRowHeight(i, 35);
    }

    tabWidgetChargingRecord.verticalHeader()->hide();
    QStringList Header;
    Header << tr("序号") << tr("订单号") << tr("开始时间") << tr("充电时长") << tr("充电电量") << tr("充电金额") << tr("结束原因") << tr("上报状态");
    tabWidgetChargingRecord.setHorizontalHeaderLabels(Header);
    tabWidgetChargingRecord.setStyleSheet("QTableWidget{border-style: solid; border-color: rgb(25, 25, 112);  color:rgb(25, 25, 112); border-width: 2px; background-color: rgb(126, 205, 244);}");
    tabWidgetChargingRecord.horizontalHeader()->setStyleSheet("background-color: rgb(126, 205, 244); color:rgb(25, 25, 112);");

    connect(stackLayout,SIGNAL(currentChanged(int)),this, SLOT(ChargeRecordShow(int)));


}

void RecordOfCharge::ChargeRecordShow(int current)
{
    if(current == recordOfChargePage && Fitrating == 0)
    {
        //充电记录倒叙查看
        QSqlDatabase db =  onInitAccount();
#if (PRODUCER == 3 || PRODUCER == 5)
        ROC_accountModel->setQuery("select * from tagAccount_xj where flag_finished != 85 order by starttime desc");
#elif (PRODUCER != 3 && PRODUCER != 1)
        ROC_accountModel->setQuery("select * from tagAccount where flag_finished != 85 order by starttime desc");
#elif (PROUDUCER == 1)
        ROC_accountModel->setQuery("select * from tagAccount_yct where flag_finished != 85 order by starttime desc");
#endif
        onAccountCuttentIndex(0);
        ROC_accountMapper->toFirst();
//        m_editChargeRecordFilter_CardNum.clear();
//        m_editChargeRecordFilter_StartTime.clear();
//        m_editChargeRecordFilter_EndTime.clear();
//        stackLayout->setCurrentIndex(recordOfChargePage);
        db.close();

    }
}
void RecordOfCharge::onAccountCuttentIndex(int index)
{
    while(ROC_accountModel->canFetchMore())
    {
        ROC_accountModel->fetchMore();
    }
    ROC_accountModel->fetchMore();
    int m_record = ROC_accountModel->rowCount();

    qDebug() << "xxxxxxxxxxxxxxxx: " << m_record;

    if( m_record < 6 )
    {
        m_recordAccount = m_record;
    }
    else
    {
        m_recordAccount = 6;
    }

    for(int i=0; i< m_recordAccount; i++,index = index + 1)
    {
        if(index > m_record)
            break;
        QString over_flag = ROC_accountModel->data(ROC_accountModel->index(index, 1)).toString();
        if( over_flag.toInt() == 102 || over_flag.toInt() == 170 /*|| over_flag.toInt() == 119*/)
        {
#if (PRODUCER !=3 && PRODUCER != 5)
            QString StartTime = ROC_accountModel->data(ROC_accountModel->index(index, 3)).toString().left(10).insert(2, "-").insert(5,"-").insert(8, " ").insert(11, ":");
            QString userNum = ROC_accountModel->data(ROC_accountModel->index(index, 7)).toString();
            QString cardNum = ROC_accountModel->data(ROC_accountModel->index(index, 2)).toString();
#elif(PRODUCER ==3 || PRODUCER == 5)
            QString StartTime = ROC_accountModel->data(ROC_accountModel->index(index, 3)).toString().mid(2,10).insert(2, "-").insert(5,"-").insert(8, " ").insert(11, ":");
            QString userNum = ROC_accountModel->data(ROC_accountModel->index(index, 8)).toString();
            QString cardNum = ROC_accountModel->data(ROC_accountModel->index(index, 2)).toString();
#endif
            if( !cardNum.isEmpty() )
            {
                QTableWidgetItem *NumItem = new QTableWidgetItem(QString::number(index));
                NumItem->setTextAlignment(Qt::AlignCenter);
                tabWidgetChargingRecord.setItem(i, 0, NumItem);
            }

            QString overReason = ROC_accountModel->data(ROC_accountModel->index(index, 10)).toString();

            qDebug()<<">>>>>>>>>userNumuserNumuserNum<<<<<<<<<<<<<"<<userNum;
            qDebug()<<">>>>>>>>>>>>>>>>>>>>cardNumcardNum<<<<<<<<<<<<<<<<<<<<<<<<"<<cardNum;
//            if( userNum.trimmed() == "ffffffffffffffff" )
//            {
                QTableWidgetItem *cardNumItem = new QTableWidgetItem(/*reverse(*/cardNum/*)*/);
                cardNumItem->setTextAlignment(Qt::AlignCenter);
                tabWidgetChargingRecord.setItem(i, 1, cardNumItem);
//            }
//            else if( cardNum.trimmed() == "ffffffffffffffff" )
//            {
//                QTableWidgetItem *cardNumItem = new QTableWidgetItem(reverse(userNum));
//                cardNumItem->setTextAlignment(Qt::AlignCenter);
//                tabWidgetChargingRecord.setItem(i, 1, cardNumItem);
//            }

              if( !overReason.isEmpty() )
              {
                  QTableWidgetItem *startTimeItem = new QTableWidgetItem(StartTime);
                  startTimeItem->setTextAlignment(Qt::AlignCenter);
                  tabWidgetChargingRecord.setItem(i, 2, startTimeItem);
              }

              if( !ROC_accountModel->data(ROC_accountModel->index(index, 5)).toString().isEmpty()  )
              {
                  float m_chargingTime = ROC_accountModel->data(ROC_accountModel->index(index, 5)).toFloat();
                  QTableWidgetItem *chargingTimeItem = new QTableWidgetItem(QString::number(m_chargingTime));
                  chargingTimeItem->setTextAlignment(Qt::AlignCenter);
                  tabWidgetChargingRecord.setItem(i, 3, chargingTimeItem);
              }

#if (PRODUCER != 3 && PRODUCER != 5)
              if( !ROC_accountModel->data(ROC_accountModel->index(index, 22)).toString().isEmpty() )
#else
                  if( !ROC_accountModel->data(ROC_accountModel->index(index, 13)).toString().isEmpty() )
#endif
               {
#if(PRODUCER != 3 && PRODUCER != 5)
                   float m_chargingPower = ( ROC_accountModel->data(ROC_accountModel->index(index, 22)).toFloat() ) ;
#else
                   float m_chargingPower = ( ROC_accountModel->data(ROC_accountModel->index(index, 13)).toFloat() ) ;
#endif

#if (PRODUCER == 3)
                   QTableWidgetItem *ChargingPowerItem = new QTableWidgetItem(QString::number(m_chargingPower * 1.0/1000, 'f', 3));
#else
                   QTableWidgetItem *ChargingPowerItem = new QTableWidgetItem(QString::number(m_chargingPower/100));
#endif
                   ChargingPowerItem->setTextAlignment(Qt::AlignCenter);
                   tabWidgetChargingRecord.setItem(i, 4, ChargingPowerItem);
               }
#if(PRODUCER != 3 && PRODUCER != 5)
                  if( !ROC_accountModel->data(ROC_accountModel->index(index, 27)).toString().isEmpty() )
               {
                   float m_changingMoney = ( ROC_accountModel->data(ROC_accountModel->index(index, 27)).toFloat() ) / 100;
                   QTableWidgetItem *ChargingMoneyItem = new QTableWidgetItem(QString::number(m_changingMoney));
                   ChargingMoneyItem->setTextAlignment(Qt::AlignCenter);
                   tabWidgetChargingRecord.setItem(i, 5, ChargingMoneyItem);
               }
#endif
           onDeviceStopReason(overReason.toInt(), ROC_accountModel->data(ROC_accountModel->index(index, 68)).toFloat());
           QTableWidgetItem *overReasonItem = new QTableWidgetItem(m_overReason);
           overReasonItem->setTextAlignment(Qt::AlignCenter);
           tabWidgetChargingRecord.setItem(i, 6, overReasonItem);

           if( over_flag.toInt() == 102 /*|| over_flag.toInt() == 119*/)
           {
               QTableWidgetItem *over_FlagItem = new QTableWidgetItem(QString("未上报"));
               over_FlagItem->setTextAlignment(Qt::AlignCenter);
               tabWidgetChargingRecord.setItem(i, 7, over_FlagItem);
           }
           else if( over_flag.toInt() == 170 )
           {
               QTableWidgetItem *over_FlagItem = new QTableWidgetItem(QString("已上报"));
               over_FlagItem->setTextAlignment(Qt::AlignCenter);
               tabWidgetChargingRecord.setItem(i, 7, over_FlagItem);
           }
        }
        else
        {
            i--;
        }
    }
}

void RecordOfCharge::onDeviceStopReason(int n_stopReason, int stopReason_detail)
{
    switch(n_stopReason)
    {
        case 0: m_overReason = tr(" ");break;
        case 1: m_overReason = tr("刷卡停止充电");break;
        case 2: m_overReason = tr("远程停止充电");break;
        case 3: m_overReason = tr("充满自动停止");break;
        case 4: m_overReason = tr("余额不足停止");break;
        case 5: m_overReason = tr("故障%1").arg(stopReason_detail);break;
        case 6: m_overReason = tr("BMS故障停止");break;
        case 7: m_overReason = tr("急停故障停止");break;
        case 8: m_overReason = tr("验证码停止");break;
        case 9: m_overReason = tr("停电停止");break;
        case 10: m_overReason = tr("充电电量到自动停止");break;
        case 11: m_overReason = tr("充电金额到自动停止");break;
        case 12: m_overReason = tr("充电时间到自动停止");break;
        case 13: m_overReason = tr("拔枪停止");break;
        case 255: m_overReason = tr("其他");break;
    }
}

void RecordOfCharge::on_buttonFirst_clicked()
{
/*待完成*/
    tabWidgetChargingRecord.clearContents();
    onAccountCuttentIndex(0);
    ROC_accountMapper->toFirst();
}

void RecordOfCharge::on_buttonPrev_clicked()
{
/*待完成*/
    tabWidgetChargingRecord.clearContents();
    int currendIndex = ROC_accountMapper->currentIndex();
    currendIndex = currendIndex - 6;
    if( currendIndex < 0 )
    {
        ROC_accountMapper->setCurrentIndex(0);
        onAccountCuttentIndex(0);
    }
    else
    {
        ROC_accountMapper->setCurrentIndex(currendIndex);
        onAccountCuttentIndex(currendIndex);
    }

}

void RecordOfCharge::FitrateDataShow(int FiterFlag, QString serialnum, QString StartTime, QString EndTime)
{
    Fitrating = FiterFlag;

    QSqlDatabase db =  onInitAccount();
    tabWidgetChargingRecord.clearContents();
    if(  !serialnum.isEmpty() && !StartTime.isEmpty() && !EndTime.isEmpty()   )
    {
        QRegExp regExp("(\\-)|(\\s)|(:)");
        QString card = /*reverse(*/serialnum/*)*/.trimmed();
        QString start = StartTime.replace(regExp, "").trimmed();
        QString end = EndTime.replace(regExp, "").trimmed();
#if(PRODUCER != 3 && PRODUCER != 1 && PRODUCER != 5)
        QString selectCmd = QString("select * from  tagAccount  where flag_finished != 85 and serialnum = '" + card.trimmed() + "' and starttime >= '"+ start + QString("00000000") +"' and  starttime <= '" + end + QString("23596000") +  "'  order by starttime desc");
#elif(PRODUCER == 3 || PRODUCER == 5)
        QString selectCmd = QString("select * from  tagAccount_xj  where flag_finished != 85 and serialnum = '" + card.trimmed() + "' and starttime >= '"+ QString("20") + start + QString("000000") +"' and  starttime <= '" + QString("20") +end + QString("235960") +  "'  order by starttime desc");
#elif(PRODUCER == 1)
        QString selectCmd = QString("select * from  tagAccount_yct  where flag_finished != 85 and serialnum = '" + card.trimmed() + "' and starttime >= '"+ QString("20") + start + QString("000000") +"' and  starttime <= '" + QString("20") +end + QString("235960") +  "'  order by starttime desc");
#endif
        qDebug() << "Cmd: " << selectCmd;
        ROC_accountModel->setQuery(selectCmd);
    }
    else if( !serialnum.isEmpty() && !StartTime.isEmpty() && EndTime.isEmpty() )
    {
        QRegExp regExp("(\\-)|(\\s)|(:)");
        QString card = /*reverse(*/serialnum/*)*/.trimmed();
        QString start = StartTime.replace(regExp, "").trimmed();
#if (PRODUCER != 3 && PRODUCER != 1 && PRODUCER != 5)
        QString selectCmd = QString("select * from  tagAccount  where flag_finished != 85 and serialnum = '"+ card.trimmed() +"' and starttime >= '" + start + QString("00000000") +  "'  order by  starttime  desc");
#elif(PRODUCER == 3 || PRODUCER == 5)
        QString selectCmd = QString("select * from  tagAccount_xj  where flag_finished != 85 and serialnum = '"+ card.trimmed() +"' and starttime >= '" + QString("20") + start + QString("000000") +  "'  order by  starttime  desc");
#elif(PRODUCER == 1)
        QString selectCmd = QString("select * from  tagAccount_yct  where flag_finished != 85 and serialnum = '"+ card.trimmed() +"' and starttime >= '" + QString("20") + start + QString("000000") +  "'  order by  starttime  desc");
#endif
        qDebug() << "Cmd: " << selectCmd;
        ROC_accountModel->setQuery(selectCmd);
    }
    else if( !serialnum.isEmpty() && StartTime.isEmpty() && !EndTime.isEmpty() )
    {
        QRegExp regExp("(\\-)|(\\s)|(:)");
        QString card = /*reverse(*/serialnum/*)*/.trimmed();
        QString end = EndTime.replace(regExp, "").trimmed();
#if(PRODUCER != 3 && PRODUCER != 1 && PRODUCER != 5)
        QString selectCmd = QString("select * from  tagAccount  where flag_finished != 85 and serialnum = '" + card.trimmed() + "' and  starttime <= '" + end + QString("23596000") + "'  order by  starttime  desc");
#elif(PRODUCER == 3 || PRODUCER == 5)
        QString selectCmd = QString("select * from  tagAccount_xj  where flag_finished != 85 and serialnum = '" + card.trimmed() + "' and  starttime <= '" + QString("20") + end + QString("235960") + "'  order by  starttime  desc");
#elif(PRODUCER == 1)
        QString selectCmd = QString("select * from  tagAccount_yct  where flag_finished != 85 and serialnum = '" + card.trimmed() + "' and  starttime <= '" + QString("20") + end + QString("235960") + "'  order by  starttime  desc");
#endif
        qDebug() << "Cmd: " << selectCmd;
        ROC_accountModel->setQuery(selectCmd);
    }
    else if( !serialnum.isEmpty() && ( StartTime.isEmpty() && EndTime.isEmpty() ) )
    {
        QString card = /*reverse(*/serialnum/*)*/.trimmed();
#if(PRODUCER != 3 && PRODUCER != 1 && PRODUCER != 5)
        QString selectCmd = QString("select * from  tagAccount  where flag_finished != 85 and serialnum = '" + card.trimmed() + "' order by  starttime  desc");
#elif(PRODUCER == 3 || PRODUCER == 5)
        QString selectCmd = QString("select * from  tagAccount_xj  where flag_finished != 85 and serialnum = '" + card.trimmed() + "' order by  starttime  desc");
#elif(PRODUCER == 1)
        QString selectCmd = QString("select * from  tagAccount_yct  where flag_finished != 85 and serialnum = '" + card.trimmed() + "' order by  starttime  desc");
#endif
        qDebug() << "Cmd: " << selectCmd;
        ROC_accountModel->setQuery(selectCmd);
    }
    else if(  serialnum.isEmpty() && !StartTime.isEmpty() && !EndTime.isEmpty()   )
    {
        QRegExp regExp("(\\-)|(\\s)|(:)");
        QString start = StartTime.replace(regExp, "").trimmed();
        QString end = EndTime.replace(regExp, "").trimmed();
#if(PRODUCER != 3 && PRODUCER != 1 && PRODUCER != 5)
        QString selectCmd = QString("select * from  tagAccount  where flag_finished != 85 and starttime >= '"+ start + QString("00000000") +"' and  starttime <= '" + end + QString("23596000") +  "'  order by starttime desc");
#elif(PRODUCER == 3 || PRODUCER == 5)
        QString selectCmd = QString("select * from  tagAccount_xj  where flag_finished != 85 and starttime >= '"+ QString("20") + start + QString("000000") +"' and  starttime <= '" + QString("20") + end + QString("235960") +  "'  order by starttime desc");
#elif(PRODUCER == 1)
        QString selectCmd = QString("select * from  tagAccount_yct  where flag_finished != 85 and starttime >= '"+ QString("20") + start + QString("000000") +"' and  starttime <= '" + QString("20") + end + QString("235960") +  "'  order by starttime desc");
#endif
        qDebug() << "Cmd: " << selectCmd;
        ROC_accountModel->setQuery(selectCmd);
    }
    onAccountCuttentIndex(0);
    ROC_accountMapper->toFirst();
//    m_editChargeRecordFilter_CardNum.clear();
//    m_editChargeRecordFilter_StartTime.clear();
//    m_editChargeRecordFilter_EndTime.clear();
    stackLayout->setCurrentIndex(recordOfChargePage);
    db.close();

    Fitrating = 0;
}
void RecordOfCharge::on_buttonNext_clicked()
{
/*待完成*/
    tabWidgetChargingRecord.clearContents();
    int currendIndex = ROC_accountMapper->currentIndex();
    qDebug("ROC_accountMapper->currentIndex() ==== %d\n ", currendIndex);
    currendIndex = currendIndex + 6;
    onAccountCuttentIndex(currendIndex);
    ROC_accountMapper->setCurrentIndex(currendIndex);

}

void RecordOfCharge::on_buttonSelect_clicked()
{
/*待完成*/
    stackLayout->setCurrentIndex(filterRecordOfChargePage);
}

void RecordOfCharge::on_buttonBack_clicked()
{
    stackLayout->setCurrentIndex(SystemSetting4Page);
}

RecordOfCharge::~RecordOfCharge()
{
    delete ui;
}
